package my.game.Descension;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class LevelLoader implements Constants{

	String nextline;
	boolean mapLoaded;
	int[] doorDestination; // Where door takes player to.
	int[] door2Destination; // Where cave door takes player to. Only on maps with 2 doors.
						  // Which is overworld and the first cave map.
	String firstDoor;
	String secondDoor;
	String info; // Info for a tile...
	String curLevel; // Current level.
	private int solidity[];
	private Sprite tiles[];
	private ArrayList<Sprite> doors;
	
	public LevelLoader(String level) throws Exception {
		doors = new ArrayList<Sprite>();
		curLevel = level;
		solidity = new int[400];
		tiles = new Sprite[400];
		char mapString[] = new char[400];
		doorDestination = new int[2];
		door2Destination = new int[2];
		mapLoaded = false;
		
		String map = "levels/"+level + ".txt";
		
		for (int i = 0; i < 400; i++) {
			solidity[i] = 0;
			tiles[i] = null;
		}
		
		if (!mapLoaded) {
			BufferedReader mapBuffer = new BufferedReader(new FileReader(map));
			int inc = 0;
			for (int k = 0; k < 20; k++) {
				char foostring[] = mapBuffer.readLine().toCharArray();
				
				for (int j = 0; j < 20; j++) {
					mapString[inc] = foostring[j];
					inc++;
				}
			}
			
			this.firstDoor = mapBuffer.readLine();
			String firstcoord = mapBuffer.readLine();
			String secondcoord = mapBuffer.readLine();
			
			
			doorDestination[0] = Integer.parseInt(firstcoord);
			doorDestination[1] = Integer.parseInt(secondcoord);
			this.secondDoor = mapBuffer.readLine();
			firstcoord = mapBuffer.readLine();
			secondcoord = mapBuffer.readLine();
			door2Destination[0] = Integer.parseInt(firstcoord);
			door2Destination[1] = Integer.parseInt(secondcoord);
			
			mapBuffer.close();
		}
		
		for (int jk = 0; jk < mapString.length; jk++) {
			
			tiles[jk] = new Sprite("null"); // Default case;
			
			if (mapString[jk] == 'w') {
				tiles[jk] = new Sprite("water");
				tiles[jk].setPassable(false);
				
			}
			
			if (mapString[jk] == ' ') {
				tiles[jk] = new Sprite("grass");
				tiles[jk].setPassable(true);
			}
			
			if (mapString[jk] == 'g') {
				tiles[jk] = new Sprite("grass");
				tiles[jk].setPassable(true);
			}
			
			if (mapString[jk] == 't') {
				tiles[jk] = new Sprite("tree1");
				tiles[jk].setPassable(false);
			}
			
			// TODO: Unhardcode these so doors can go both ways.
			// TODO: Unhardcode Info
			if (mapString[jk] == 'd') {
				tiles[jk] = new Sprite("door");
				tiles[jk].setPassable(true);
				tiles[jk].setInfo("Town");
				tiles[jk].setDoor(true, firstDoor, doorDestination);
				tiles[jk].setIsDoor1(true);
				doors.add(tiles[jk]);
			}
			
			if (mapString[jk] == 'q') {
				tiles[jk] = new Sprite("skulls");
				tiles[jk].setPassable(false);
			}
			
			if (mapString[jk] == 'D') {
				tiles[jk] = new Sprite("cave_door");
				tiles[jk].setPassable(true);
				tiles[jk].setInfo("Cave Entrance");
				tiles[jk].setDoor(true, secondDoor, door2Destination);
				tiles[jk].setIsDoor1(false);
				doors.add(tiles[jk]);
			}
			
			// TODO: Add AI to the sprites
			if (mapString[jk] == '$') {
				tiles[jk] = new Sprite("slug");
				tiles[jk].setPassable(false);
				
			}
			
		}
		
		int tally = 0;
		for (int l = 0; l < 20; l++) {
			for (int p = 0; p < 20; p++) {
				tiles[tally].setX(p);
				tiles[tally].setY(l);
				tally++;
			}
		}
		
	}
	
	
	public Sprite[] getTiles() {
		return tiles;
	}
	
	
	public Sprite findSprite(String door) {
		
		//System.out.println("Door: "+door);
		//System.out.println("DoorLocation: "+doorDestination);
		//System.out.println("Door2Location: "+door2Destination);
		
		/*for (int i = 0; i < doors.size(); i++) {
			//if(door == doorLocation) {
			if (door == doorDestination) {
				if (doors.get(i).newLocation() == doorDestination)
				return doors.get(i);
					
			}
			if (door == door2Location) {
				if(doors.get(i).newLocation() == door2Destination)
					return doors.get(i);
			}
		}
		*/
		Sprite nullSprite = new Sprite("null");
		
		//System.out.println("Didn't find sprite");
		//nullSprite.setDoor(true);
		
		return nullSprite;
	}
	
	public String getCurrentLevelName() {
		return curLevel;
	}
	
	// Returns sprite at specified coordinates
	public Sprite findSprite(int x, int y) {
		for (int i = 0; i < tiles.length; i++) {
			if(tiles[i].getX() == x && tiles[i].getY() == y)
				return tiles[i];
		}
		
		Sprite nullSprite = new Sprite("null");
		nullSprite.setPassable(false);
		return nullSprite;
	}
}
